package org.honeyweb.gsoc.services.impl;

import java.util.ArrayList;
import java.util.Date;

import org.honeyweb.gsoc.dao.HoneypotsDao;
import org.honeyweb.gsoc.model.Honeypots;
import org.honeyweb.gsoc.model.Organizations;
import org.honeyweb.gsoc.services.HoneypotsManager;

public class HoneypotsManagerImpl implements HoneypotsManager {
	
    private HoneypotsDao honeypotsDao = null;
    
	/**
     * setter to allows spring to inject honeypotsDao implementation
     * @param honeypotsDao : object (implementation of HoneypotsDao interface) to inject.
     */
    public void setHoneypotsDao(HoneypotsDao honeypotsDao) {
		this.honeypotsDao = honeypotsDao;
	}
    
	public ArrayList<Honeypots> getHoneypots(Organizations organization){
		
		if(organization != null){
			//Get honeypots Ids from the organization
			String honeypotsId = organization.getHoneypotsId();
			if(honeypotsId != null ){
				String[] honeypotsTable = honeypotsId.split(",");
				ArrayList<Integer> honeypotsIdList = new ArrayList<Integer>();
				for (String s : honeypotsTable ){
					honeypotsIdList.add(Integer.parseInt(s));
				}
				
				//Get the honeypots list from Ids 
				ArrayList<Honeypots> honeypotsList = new ArrayList<Honeypots>();		
				for (Integer i : honeypotsIdList ){
					honeypotsList.add(honeypotsDao.getHoneypot(i));
				}
				
				return honeypotsList;
			}
			else{
				return null;
			}
		}
		else{
			return null;
		}
		
	}
	
    /**
     * {@inheritDoc}
     */
    public Honeypots addHoneypot(String name,String url,String country) {
        Honeypots honeypot = new Honeypots(url,name,country,"Available");
        honeypotsDao.addHoneypot(honeypot);    
        return honeypot;
    }
    
    /**
     * {@inheritDoc}
     */
    public Honeypots delHoneypot(int honeypotId) {
    	Honeypots honeypot = honeypotsDao.getHoneypot(honeypotId);
        honeypotsDao.delHoneypot(honeypot); 
        return honeypot;
    }

}
